﻿using MediatR;
using Sale.Api.Application.工单.Command.OpenFare;
using Sale.Domain.工单;
using Sale.ErrorCode;
using Sale.Infrastructure.Interface;

namespace Sale.Api.Application.工单.Handler.OpenFare
{
    public class GetOpenFareHandler : IRequestHandler<GetOpenFareCommand, ApiPaging<OpenFareModel>>
    {
        private readonly IBaseRepository<OpenFareModel> openFareRepo;
        private readonly ILogger<GetOpenFareHandler> logger;

        public GetOpenFareHandler(IBaseRepository<OpenFareModel> openFareRepo,ILogger<GetOpenFareHandler> logger)
        {
            this.openFareRepo = openFareRepo;
            this.logger = logger;
        }
        public Task<ApiPaging<OpenFareModel>> Handle(GetOpenFareCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<OpenFareModel> paging=new ApiPaging<OpenFareModel>();
            var openFare = openFareRepo.GetAll().Where(x=>x.IsDel==false);
            paging.TotalCount = openFare.Count();
            paging.TotalPCount = (int)Math.Ceiling((paging.TotalCount * 1.0) / request.size);
            paging.Data = openFare.OrderBy(x => x.OpenFareId).Skip((request.index - 1) * request.size).Take(request.size).ToList();
           
            paging.Code = ApiEnum.OK;
            paging.Msg = "开票管理列表显示成功";
            logger.LogInformation(paging.Msg);
            return Task.FromResult(paging);
        }
    }
}
